; (function (window, $) {
  $(function () {
    // enter
    $(".usertype-select-box a").on('click',function(){
      $(".usertype-select-box").fadeOut(200);
      if (parseInt($(this).data("type"))===1){
        $("a.usertype-text").text("用户注册");
        $(".data-show,#one").fadeIn(200);
      }else
      {
        $("a.usertype-text").text("渠道注册");
        $(".data-show,#two").fadeIn(200);
      }
    })

    $("a.type-back").on('click',function(){
      $(".data-show,#one,#two").fadeOut(200);
      $(".usertype-select-box").fadeIn(200);
    })

    // global
    var member_can_reg = true
    var manager_can_reg = true
    var member_canSendCode = true;
    var member_timer = 0;
    var member_second = 0;

    var manager_canSendCode = true;
    var manager_timer = 0;
    var manager_second = 0;

    // ------member-----------

    // xieyi check
    $('#member-check').on('click', function () {
      if ($(this).prop('checked')) {
        member_can_reg = true
        $('#member-reg').css('background-color', '#6f81dd')
      } else {
        member_can_reg = false
        $('#member-reg').css('background-color', '#B6B6B6')
      }
    })

    //send code
    $('#member-getcode').on('click', function () {
      if ($('#member-username').val() == '') {
        layer.alert('请输入手机号码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }

      var phone = $('#member-username').val()

      $.ajax({
        url: util.host + '/auth/checkValidPhone/' + phone,
        type: 'POST',
        success: function (res) {

          if (res.success) {
            if (member_canSendCode) {
              member_second = 60;
              member_canSendCode = false;
              $('#member-getcode').val('剩余' + member_second + '秒');
              member_timer = setInterval(function () {
                if (member_second < 1) {
                  clearInterval(member_timer);
                  $('#member-getcode').val('获取验证码');
                  member_canSendCode = true;
                } else {
                  member_second--;
                  $('#member-getcode').val('剩余' + member_second + '秒');
                }
              }, 1000);
              sendCode(phone)
            } else {
              return;
            }
          } else {
            layer.alert('该手机号码已被注册', {
              skin: 'demo-class',
              closeBtn: 0
            });
          }

        },
        error: function (res) {
          console.log(res)
        }
      })
    })

    $('#member-reg').on('click', function () {
      var username = $('#member-username').val()
      var psw = $('#member-password').val()
      var code = $('#member-code').val()

      if (!member_can_reg) {
        layer.alert('请同意平台注册协议', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (username == '') {
        layer.alert('请输入手机号码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      } else if (username.length < 11) {
        layer.alert('请输入正确的手机号码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (psw.length<6 || psw.length>16) {
        layer.alert('密码长度需要大于6个字符且小于16个字符', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (!psw.match(/^[A-Za-z0-9\\\\u4e00-\\u9fa5]+$/gi)){
        layer.alert('密码中不能包含特殊字符', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      } else if (psw.length < 6) {
        layer.alert('密码至少6位数', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (code == '') {
        layer.alert('请输入验证码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }

      var sendobj = {
        phone: username,
        smsCode: code,
        deviceId: username,
        loginPwd: psw,
        confirmPwd: psw,
        registerSource: 1,
        userType: 1
      }

      $.ajax({
        url: util.host + '/auth/register' + util.obj2query(sendobj),
        type: 'POST',
        contentType: 'application/json',
        success: function (res) {
          console.log(res)
          layer.alert('注册成功，现在进入登录页面', {
            skin: 'demo-class',
            closeBtn: 0
          }, function () {
            window.location.href = '/login.html'
          });
        },
        error: function (err) {
          layer.alert(JSON.parse(err.responseText).message, {
            skin: 'demo-class',
            closeBtn: 0
          });
        }
      })
    })
    // ------member-----------

    // ------manager-----------

    // xieyi check
    $('#manager-check').on('click', function () {
      if ($(this).prop('checked')) {
        manager_can_reg = true
        $('#manager-reg').css('background-color', '#6f81dd')
      } else {
        manager_can_reg = false
        $('#manager-reg').css('background-color', '#B6B6B6')
      }
    })

    //send code
    $('#manager-getcode').on('click', function () {
      if ($('#manager-username').val() == '') {
        layer.alert('请输入手机号码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }

      var phone = $('#manager-username').val();

      $.ajax({
        url: util.host + '/auth/checkValidPhone/' + phone,
        type: 'POST',
        success: function (res) {
          if (res.success) {
            if (manager_canSendCode) {
              manager_second = 60;
              manager_canSendCode = false;
              $('#manager-getcode').val('剩余' + manager_second + '秒');
              manager_timer = setInterval(function () {
                if (manager_second < 1) {
                  clearInterval(manager_timer);
                  $('#manager-getcode').val('获取验证码');
                  manager_canSendCode = true;
                } else {
                  manager_second--;
                  $('#manager-getcode').val('剩余' + manager_second + '秒');
                }
              }, 1000);
              sendCode(phone)
            } else {
              return;
            }
          } else {
            layer.alert('该手机号码已被注册', {
              skin: 'demo-class',
              closeBtn: 0
            });
          }
        },
        error: function (res) {
          console.log(res)
        }
      })
    })

    $('#manager-reg').on('click', function () {
      var username = $('#manager-username').val()
      var psw = $('#manager-password').val()
      var code = $('#manager-code').val()

      if (!manager_can_reg) {
        layer.alert('请同意平台注册协议', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (username == '') {
        layer.alert('请输入手机号码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (psw.length<6 || psw.length>16) {
        layer.alert('密码长度需要大于6个字符且小于16个字符', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (!psw.match(/^[A-Za-z0-9\\\\u4e00-\\u9fa5]+$/gi)){
        layer.alert('密码中不能包含特殊字符', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }
      if (code == '') {
        layer.alert('请输入验证码', {
          skin: 'demo-class',
          closeBtn: 0
        });
        return
      }

      var sendobj = {
        phone: username,
        smsCode: code,
        deviceId: username,
        loginPwd: psw,
        confirmPwd: psw,
        registerSource: 1,
        userType: 2
      }

      $.ajax({
        url: util.host + '/auth/register' + util.obj2query(sendobj),
        type: 'POST',
        contentType: 'application/json',
        success: function (res) {
          console.log(res)
          layer.alert('注册成功，现在进入登录页面', {
            skin: 'demo-class',
            closeBtn: 0
          }, function () {
            window.location.href = '/login.html'
          });
        },
        error: function (err) {
          var data = JSON.parse(err.responseText || {});
          if (data.message) {
            alert(data.message);
          }
        }
      })
    })
    // ------manager-----------
  })

  function sendCode(phone) {
    $.ajax({
      url: util.host + '/auth/code/sms',
      type: 'GET',
      data: {
        deviceId: phone,
        phone: phone
      },
      success: function (res) {
        console.log(res)
        console.log('code is send')
        layer.alert('验证码已发送', {
          skin: 'demo-class',
          closeBtn: 0
        });
      }
    })
  }
})(window, jQuery)
